* Replication file for "Not All Elections Are Created Equal: Elections Quality and Civil Conflict"
* Authors: Daniela Donno (University of Cyprus), Kelly Morrison (University of Pittsburgh), and Burcu Savun (University of Pittsburgh)


use "data.dta"

* TABLE 1
tab onset frfair_dummy if nelda_preselec==1, col chi2


* TABLE 2

* model 1
logit onset nelda_preselec history_conflict nelda_viol partylost nelda_legelec ldiscrimpop lngdppc logpop ln_demaid polity2 peaceyears peaceyears2 peaceyears3, vce(cluster ccode)

* model 2
firthlogit onset nelda_preselec frfair_rescale_pres history_conflict nelda_viol partylost nelda_legelec ldiscrimpop lngdppc logpop ln_demaid polity2 peaceyears peaceyears2 peaceyears3

* model 3
logit onset nelda_preselec frfair_rescale_pres history_conflict nelda_viol partylost nelda_legelec ldiscrimpop lngdppc logpop ln_demaid polity2 peaceyears peaceyears2 peaceyears3 i.ccode i.year, vce(cluster ccode)   

* model 4
logit onset frfair_rescale history_conflict nelda_viol partylost nelda_legelec ldiscrimpop lngdppc logpop ln_demaid polity2 peaceyears peaceyears2 peaceyears3 if (nelda_preselec==1), vce(cluster ccode)

* model 5
logit onset frfair_rescale history_conflict nelda_viol partylost nelda_legelec ldiscrimpop lngdppc logpop ln_demaid polity2 peaceyears peaceyears2 peaceyears3 if (nelda_preselec == 1 & nelda1=="no" & nelda2 == "no"), vce(cluster ccode) 

* model 6
ivprobit onset (frfair_rescale = indjud_vdem indmed_vdem indcso_vdem v2eldommon v2elintmon) history_conflict nelda_viol partylost ldiscrimpop lngdppc logpop polity2 ln_demaid peaceyears peaceyears2 peaceyears3 if nelda_preselec==1, nolog vce(cluster ccode)

* model 7 
firthlogit onset nelda_preselec history_conflict frfair_rescale_pres hist_confXffpres nelda_viol partylost nelda_legelec ldiscrimpop lngdppc logpop polity2 ln_demaid peaceyears peaceyears2 peaceyears3


* FIGURE 1
firthlogit onset nelda_preselec frfair_rescale_pres history_conflict nelda_viol partylost nelda_legelec ldiscrimpop lngdppc logpop ln_demaid polity2 peaceyears peaceyears2 peaceyears3
margins, dydx(nelda_preselec) at (frfair_rescale_pres = (1(.5)6.5)) expression(invlogit(predict(xb)))
marginsplot, level(90) ///
	graphregion(fcolor(white)) plotopts(mlcolor(black) mfcolor(black)) ciopts(lcolor(black)) ///
	title("") ytitle("Marginal Effect") xtitle("Electoral Integrity") legend(off) ///
	addplot(histogram frfair_rescale_pres if frfair_rescale_pres!=0, below fcolor(gs15) lcolor(gs13) fraction xlabel(1(.5)6.5))

	
* FIGURE 2
firthlogit onset i.nelda_preselec i.history_conflict##c.frfair_rescale_pres nelda_viol partylost nelda_legelec ldiscrimpop lngdppc logpop polity2 ln_demaid peaceyears peaceyears2 peaceyears3
margins, at (frfair_rescale_pres = 1.78 history_conflict = 0 nelda_preselec == 1) predict() level(90) expression(invlogit(predict(xb))) post
estimates store noff_nohist

firthlogit onset i.nelda_preselec i.history_conflict##c.frfair_rescale_pres nelda_viol partylost nelda_legelec ldiscrimpop lngdppc logpop polity2 ln_demaid peaceyears peaceyears2 peaceyears3
margins, at (frfair_rescale_pres = 1.78 history_conflict = 1 nelda_preselec == 1) predict() level(90) expression(invlogit(predict(xb))) post
estimates store noff_hist

firthlogit onset i.nelda_preselec i.history_conflict##c.frfair_rescale_pres nelda_viol partylost nelda_legelec ldiscrimpop lngdppc logpop polity2 ln_demaid peaceyears peaceyears2 peaceyears3
margins, at (frfair_rescale_pres = 5.49 history_conflict = 0 nelda_preselec == 1) predict() level(90) expression(invlogit(predict(xb))) post
estimates store ff_nohist

firthlogit onset i.nelda_preselec i.history_conflict##c.frfair_rescale_pres nelda_viol partylost nelda_legelec ldiscrimpop lngdppc logpop polity2 ln_demaid peaceyears peaceyears2 peaceyears3
margins, at (frfair_rescale_pres = 5.49 history_conflict = 1 nelda_preselec == 1) predict() level(90) expression(invlogit(predict(xb))) post
estimates store ff_hist

coefplot ///
	(noff_nohist, rename(_cons = "No History of Conflict ") ciopts(recast(rcap) lcolor(black)) mcolor(black)) ///
	(noff_hist, rename(_cons = "History of Conflict ") ciopts(recast(rcap) lcolor(black)) mcolor(black)) ///
	(ff_nohist, rename(_cons = "No History of Conflict") ciopts(recast(rcap) lcolor(black)) mcolor(black)) ///
	(ff_hist, rename(_cons = "History of Conflict") ciopts(recast(rcap) lcolor(black)) mcolor(black)) ///
	, xline(0, lpattern(dash) lwidth(thin) lcolor(gray)) xtitle(Predicted Probabilities) graphregion(fcolor(white)) nokey level(90) ///
	  transform(* = min(max(@,0),1)) headings("No History of Conflict " = "{bf: Low Electoral Integrity}" ///
	  "No History of Conflict" = "{bf: High Electoral Integrity}")
	  
	  
* TABLE A1
* make sure to install package "sutex2": run "help sutex2" and then click link to install package
sutex2 onset nelda_preselec frfair_rescale_pres history_conflict nelda_viol partylost nelda_legelec  /// 
	ldiscrimpop lngdppc logpop ln_demaid polity2 peaceyears pko regular /// 
	indjud_vdem indmed_vdem indcso_vdem v2eldommon v2elintmon downatall powershare maxhighx /// 
	maxlowx democratizing protests boycotts, minmax varlabels


* FIGURE A1
histogram frfair_rescale if nelda_preselec == 1, ///
	xtitle("Electoral Integrity") graphregion(fcolor(white))

	
* TABLE A2

* model 1 
firthlogit onset nelda_preselec frfair_rescale_pres history_conflict nelda_viol partylost nelda_legelec ldiscrimpop lngdppc logpop ln_demaid polity2 peaceyears peaceyears2 peaceyears3 downatall powershare maxhighx maxlowx 

* model 2
firthlogit onset nelda_preselec frfair_rescale_pres history_conflict nelda_viol partylost nelda_legelec ldiscrimpop lngdppc logpop ln_demaid polity2 peaceyears peaceyears2 peaceyears3 pko 

* model 3 
firthlogit onset nelda_preselec frfair_rescale_pres history_conflict nelda_viol partylost nelda_legelec ldiscrimpop lngdppc logpop ln_demaid polity2 peaceyears peaceyears2 peaceyears3 downatall powershare maxhighx maxlowx pko 
	
* model 4 
firthlogit onset nelda_preselec frfair_rescale_pres history_conflict nelda_viol partylost nelda_legelec ldiscrimpop lngdppc logpop ln_demaid polity2 peaceyears peaceyears2 peaceyears3 if democratizing==1


* TABLE A3

* model 1
logit boycotts frfair_rescale history_conflict ldiscrimpop lngdppc logpop polity ln_demaid, vce(cluster ccode)

* model 2
logit protests frfair_rescale history_conflict partylost ldiscrimpop lngdppc logpop polity ln_demaid, vce(cluster ccode) 


* TABLE A4

* model 1
logit onset partylost history_conflict nelda_viol ldiscrimpop lngdppc logpop polity2 ln_demaid peaceyears peaceyears2 peaceyears3 if (nelda_preselec==1), vce(cluster ccode)

* model 2
logit onset partylost frfair_rescale partylostXfrfair_rescale history_conflict nelda_viol ldiscrimpop lngdppc logpop polity2 ln_demaid peaceyears peaceyears2 peaceyears3 if (nelda_preselec==1), vce(cluster ccode)


* FIGURE A2
logit onset c.frfair_rescale##i.partylost history_conflict nelda_viol ldiscrimpop lngdppc logpop polity2 ln_demaid peaceyears peaceyears2 peaceyears3 if (nelda_preselec==1), vce(cluster ccode) 
margins, dydx(partylost) at (frfair_rescale = (.5 (0.5) 7)) 
marginsplot, level(90) ///
	graphregion(fcolor(white)) ///
	title("Marginal Effect of Party Loss") ytitle("Marginal Effect") xtitle("Electoral Integrity")
	

* TABLE A5
ivprobit onset (frfair_rescale = indjud_vdem indmed_vdem indcso_vdem v2eldommon v2elintmon) history_conflict nelda_viol partylost ldiscrimpop lngdppc logpop polity2 ln_demaid peaceyears peaceyears2 peaceyears3 if nelda_preselec==1, nolog vce(cluster ccode) first
ivregress 2sls onset (frfair_rescale = indjud_vdem indmed_vdem indcso_vdem v2eldommon v2elintmon) history_conflict nelda_viol partylost ldiscrimpop lngdppc logpop polity2 ln_demaid peaceyears peaceyears2 peaceyears3 if nelda_preselec==1
estat firststage			
estat overid


* TABLE A6
keep if nelda_preselec == 1 & frfair_dummy != .

* unmatched sample 
imb nelda_viol history_conflict partylost ldiscrimpop lngdppc logpop ln_demaid polity2, treatment(frfair_dummy)

* matched sample
cem nelda_viol history_conflict partylost ldiscrimpop(#3) lngdppc(#3) logpop(#3) ln_demaid(#3) polity2(#3), treatment(frfair_dummy)


* TABLE A7
logit onset frfair_rescale history_conflict nelda_viol partylost ldiscrimpop lngdppc logpop polity2 ln_demaid peaceyears peaceyears2 peaceyears3 [iweight = cem_weights], vce(cluster ccode) 


* TABLE A8
use "data.dta", clear
firthlogit onset nelda_preselec history_conflict frfair_rescale_pres hist_confXpreselec hist_confXffpres nelda_viol partylost nelda_legelec ldiscrimpop lngdppc logpop polity2 ln_demaid peaceyears peaceyears2 peaceyears3
